#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 20250417
@author: wangyk
"""

import argparse
import json
import os
import sys
from pathlib import Path
import subprocess


PREDICT_CENTER_COORDINATE = "/Pub/Users/wangyk/Project_wangyk/Codelib_YK/codeLibrary_yk/R/Convertional_Transcriptome/Molecular_Docking_cmd/v1.1/0.predict_center_coordinate/1.src.py"

AUTO_BOX = '/Pub/Users/wangyk/Project_wangyk/Codelib_YK/codeLibrary_yk/R/Convertional_Transcriptome/Molecular_Docking_cmd/v1.1/1.autobox_py/1.src.py'

PDB_2_PDBQT = "/Pub/Users/wangyk/Project_wangyk/Codelib_YK/codeLibrary_yk/R/Convertional_Transcriptome/Molecular_Docking_cmd/v1.1/2.pdb2pdbqt/1.src.py"

DOCKING = "/Pub/Users/wangyk/Project_wangyk/Codelib_YK/codeLibrary_yk/R/Convertional_Transcriptome/Molecular_Docking_cmd/docking"


def main(args):
    try:
        cmd1 = [PREDICT_CENTER_COORDINATE, args.ligand_pdb]
        subprocess.run(cmd1, check=True)

        cmd2 = [AUTO_BOX, args.ligand_pdb, '-p',
                f'{Path(args.ligand_pdb)}_predictions.csv']
        subprocess.run(cmd2, check=True)

        cmd3 = [PDB_2_PDBQT, Path(
            args.ligand_pdb), f"{Path(args.ligand_pdb)}_docking_center_and_autobox_info.json"]
        subprocess.run(cmd3, check=True)

        pdb_file_path = Path(args.ligand_pdb)
        cmd4 = [DOCKING,
                "-p", f'{pdb_file_path.parent/pdb_file_path.stem}_prepared.pdbqt',
                '-c', f'{pdb_file_path.parent/pdb_file_path.stem}_prepared.box.txt',
                "-od", f'{args.output_dir}']
        subprocess.run(cmd4, check=True)

    except subprocess.CalledProcessError as e:
        print(f"错误: 子命令执行失败: {e}", file=sys.stderr)
        sys.exit(1)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="计算对接分子中心坐标.",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter
    )
    parser.add_argument("ligand_pdb", help="PDB文件路径")
    parser.add_argument("output_dir", help="结果输出路径", default='.')
    args = parser.parse_args()
    try:
        main(args)
    except Exception as e:
        print(f"错误: {str(e)}", file=sys.stderr)
        sys.exit(1)
